<?php

namespace Blog\App\Model;

use Hx\App;

/**
 * Blog模型操作类
 * Class Blog
 * @package Blog\App\Model
 */
class Blog {

    /**
     * 数据库对象
     * @var \Hx\Db
     */
    private $db;

    /**
     * 构造函数
     */
    public function __construct() {
        $this->db = App::Db();
    }

    /**
     * 获取热评文章
     * @param int $size 数量
     * @return mixed
     */
    public function getPopular($size = 4) {
        $sql = App::Sql('HxSite_Node', 'title,nodeKey,commentCount')
            ->where('nodeType = "blog" AND status = "public"')
            ->order('commentCount DESC,id DESC')
            ->limit($size)
            ->select();
        return $this->db->queryAll($sql);
    }

    /**
     * 获取列表
     * @param null|string $condition 附加条件
     * @param int $page 页码
     * @param int $size 每页数量
     * @return mixed
     */
    public function getPaged($condition = null, $page, $size) {
        $col = 'title,nodeKey,N.createDate AS createDate,N.desc AS desc,categoryId,userId,commentCount';
        $col .= ',C.name AS categoryName,C.keyString AS categoryKey';
        $col .= ',U.nickName AS userName';
        $sql = App::Sql('HxSite_Node AS N', $col)
            ->leftJoin('HxSite_Category AS C', 'N.categoryId = C.id')
            ->leftJoin('HxSite_User AS U', 'U.id = N.userId')
            ->where('nodeType = "blog" AND N.status = "public" AND ' . $condition)
            ->order('createDate DESC')
            ->page($page, $size)
            ->select();
        $data['blog'] = $this->db->queryAll($sql);
        $sql = App::Sql('HxSite_Node')
            ->where('nodeType = "blog" AND status = "public" AND ' . $condition)
            ->order('createDate DESC')
            ->count();
        $all = $this->db->query($sql)->num;
        $data['count'] = array(
            'all' => $all,
            'pages' => (int)($all / $size + 0.999),
            'current' => $page,
            'begin' => ($page - 1) * $size >= $all ? $all : ($page - 1) * $size + 1,
            'end' => $page * $size >= $all ? $all : $page * $size
        );
        return $data;
    }

    /**
     * 获取所有
     * @param bool $monthly 按月份分割
     * @return array|mixed
     */
    public function getAll($monthly = false) {
        $sql = App::Sql('HxSite_Node', 'title,nodeKey,createDate,commentCount')
            ->order('createDate DESC')
            ->select();
        $tmpData = $this->db->queryAll($sql);
        if (!$monthly) {
            return $tmpData;
        }
        $returnData = array();
        foreach ($tmpData as $blog) {
            $month = date('Y.m', $blog->createDate);
            $returnData[$month][] = $blog;
        }
        return $returnData;
    }

    /**
     * 获取一个
     * @param string $key 索引
     * @param string $value 索引值
     * @return mixed
     */
    public function getOne($key, $value) {
        $col = 'N.id AS id,title,nodeKey,N.createDate AS createDate,N.content AS content,categoryId,userId,commentCount,hitCount';
        $col .= ',C.name AS categoryName,C.keyString AS categoryKey';
        $col .= ',U.nickName AS userName';
        $sql = App::Sql('HxSite_Node AS N', $col)
            ->leftJoin('HxSite_Category AS C', 'N.categoryId = C.id')
            ->leftJoin('HxSite_User AS U', 'U.id = N.userId')
            ->where($key . ' = :' . $key)
            ->where('nodeType = "blog" AND N.status = "public"')
            ->select();
        return $this->db->query($sql, array($key => $value));
    }

    /**
     * 更新文章访问计数
     * @param int $id
     * @return mixed
     */
    public function updateHit($id) {
        $sql = App::Sql('HxSite_Node', 'hitCount')
            ->where('id = :id')
            ->update();
        $sql = str_replace(':hitCount', 'hitCount + 1', $sql);
        return $this->db->exec($sql, array('id' => $id));
    }

    /**
     * 更新评论计数
     * @param int $id
     * @return mixed
     */
    public function updateComment($id) {
        $sql = App::Sql('HxSite_Node', 'commentCount')
            ->where('id = :id')
            ->update();
        $sql2 = App::Sql('HxSite_Comment')
            ->where('nodeId = ' . (int)$id)
            ->where('commentType = "comment" AND status = "public" AND parentId = 0')
            ->count();
        $sql = str_replace(':commentCount', '(' . $sql2 . ')', $sql);
        return $this->db->exec($sql, array('id' => $id));
    }

    /**
     * 检测文章关键字是否重复
     * @param string $key
     * @param int $id
     * @return bool
     */
    public function checkKey($key, $id) {
        $sql = App::Sql('HxSite_Node', 'nodeKey')
            ->where('id = :id')
            ->select();
        $res = $this->db->query($sql, array('id' => $id));
        if (!$res) {
            return false;
        }
        return $res->nodeKey === $key;
    }

    /**
     * 获取订阅数据
     * @param int $size
     * @return mixed
     */
    public function getFeed($size = 10) {
        $col = 'title,nodeKey,N.createDate AS createDate,N.content AS content,categoryId,userId,commentCount';
        $col .= ',C.name AS categoryName,C.keyString AS categoryKey';
        $sql = App::Sql('HxSite_Node AS N', $col)
            ->leftJoin('HxSite_Category AS C', 'N.categoryId = C.id')
            ->where('nodeType = "blog" AND N.status = "public"')
            ->order('createDate DESC')
            ->limit($size)
            ->select();
        return $this->db->queryAll($sql);
    }
}